<?php

namespace Yj\Generator\Core;

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Yj\Generator\Core\Model\EloquentModel;
use Yj\Generator\Core\Processor\ExistenceCheckerProcessor;
use Yj\Generator\Core\Processor\FieldProcessor;
use Yj\Generator\Core\Processor\NamespaceProcessor;
use Yj\Generator\Core\Processor\RelationProcessor;
use Yj\Generator\Core\Processor\TableNameProcessor;
use Yj\Generator\Core\Processor\TraitProcessor;

class ModelBuilder
{

    protected $tags = [
        TableNameProcessor::class,
        ExistenceCheckerProcessor::class,
        NamespaceProcessor::class,
        RelationProcessor::class,
        FieldProcessor::class,
        TraitProcessor::class,
    ];

    protected $processors = null;

    public function __construct()
    {
        if (is_null($this->processors)) {
            $this->processors = $this->register();
        }
    }

    private function register()
    {
        return new RewindableGenerator(function () {
            foreach ($this->tags as $abstract) {
                yield make($abstract);
            }
        }, count($this->tags));
    }

    public function createModel(Config $config)
    {
        $model = new EloquentModel();

        foreach ($this->processors as $processor) {
            $processor->process($model, $config);
        }
        return $model;
    }
}